/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        ListNode current = head;
        ListNode pre = null;
        while(current != null) {
            if(current.val == val) {
                if(pre == null) {
                    head = current.next;
                }
                else {
                    pre.next = current.next;
                }
                break;
            }
            else{
                pre = current;
                current = current.next;
            }
        }
        return head;
    }
}